package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.MessageMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Message;
import com.wanghaixin.pojo.MessageExample;
import com.wanghaixin.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/3 15:16
 * @Description:消息管理模块
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public int addMessage(Message message) {
        int i = messageMapper.insertSelective(message);
        return i;
    }

    @Override
    public void delMessage(Long id) {
        int i = messageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateMessage(Message message) {
        int update = messageMapper.updateByPrimaryKeyWithBLOBs(message);
        return update;
    }

    @Override
    public Message findById(Long id) {
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        MessageExample example = new MessageExample();
        if (!StringUtils.isEmpty(queryDTO.getSort())) {
            example.setOrderByClause("id");
        }
        List<Message> message = messageMapper.selectByExampleWithBLOBs(example);
        PageInfo<Message> carPageInfo = new PageInfo<>(message);
        long total = carPageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, message);
        return dataGridResult;
    }}


